<template>
  <div ref="dom" class="charts chart-line"></div>
</template>

<script>
import echarts from 'echarts'
import tdTheme from './theme.json'
import { on, off } from '@/libs/tools'
echarts.registerTheme('tdTheme', tdTheme)
export default {
  name: 'ChartLine',
  props: {
    allIn: Number, // 所有
    currentLeave: Number, // 当月减少
    currentNew: Number, // 当月新增
    value: Array,
    // color: Array,
    text: String,
    seriesColor: String
  },
  data () {
    return {
      dom: null
    }
  },
  methods: {
    resize () {
      this.dom.resize()
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.initLine()
    })
  },
  watch: {
    value: {
      // immediate: true,
      handler (val, oldVal) {
        this.value = val
        this.initLine()
      }
    }
  },
  methods: {
    initLine () {
      // let legend = this.value.map(_ => _.name)
      let xAxisData = this.value.map(item => {
        return item.periodYear + '-' + item.periodMonth
      })
      let seriesData = this.value.map(item => {
        return item.total
      })
      let option = {
        title: {
          text: this.text,
          textStyle: {
            color: '#333'
          },
          x: 'left'
        },
        tooltip: {
          trigger: 'item'
          // formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          name: '时间',
          nameTextStyle: {
            color: '#262626',
            fontSize: 14
          },
          data: xAxisData,
          position: 'bottom',
          splitLine: {
            show:false
          },
          axisLabel: {
            textStyle: {
              color: '#262626'
            }
          },
          axisTick: {
            lineStyle: {
              color: '#979797'
            }
          },
          axisLine: {
            lineStyle: {
              color: '#979797'
            }
          }
        },
        yAxis: {
          // min: 0,
          // max: 1200,
          splitNumber: 4,
          axisLabel: {
            textStyle: {
              color: '#262626'
            }
          },
          axisTick: {
            lineStyle: {
              color: '#eee'
            }
          },
          axisLine: {
            lineStyle: {
              color: '#979797'
            }
          },
          splitLine: {
            lineStyle: {
              color: '#eee'
            }
          }
        },
        series: [
          {
            type: 'line',
            // symbol:'circle',
            smooth: false,
            symbolSize: 10,
            lineStyle: {
              normal: {
                color: this.seriesColor
              }
            },
            itemStyle: {
              normal: {
                label: { show: true },
                color: this.seriesColor,
                borderColor: this.seriesColor
              }
            },
            data: seriesData
          }
        ]
      }
      this.dom = echarts.init(this.$refs.dom, 'tdTheme')
      this.dom.setOption(option)
      on(window, 'resize', this.resize)
    }
  },
  beforeDestroy () {
    off(window, 'resize', this.resize)
  }
}
</script>
